package servlet;

import model.Department;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@WebServlet("/editServlet.action")
public class EditPartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String id = req.getParameter("id");
        int uid = Integer.parseInt(id);
        File file = new File("C:\\Users\\FeiFei\\Desktop\\test.txt");
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        List<Department> partList = null;
        try {
            partList = (List<Department>) ois.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Department part = partList.get(uid);
        req.setAttribute("part", part);
        req.setAttribute("id", id);
        req.getRequestDispatcher("editPart.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String id = req.getParameter("id");
        int uid = Integer.parseInt(id);
        Department part=new Department();
        try {
            BeanUtils.populate(part,req.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd  HH:mm");
        //创建日期值
        Date now=new Date();
        String format = dateFormat.format(now);
        //格式化日期
        part.setUpdateTime(format);
        File file = new File("C:\\Users\\FeiFei\\Desktop\\test.txt");
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        List<Department> partList = null;
        try {
            partList = (List<Department>) ois.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        partList.set(uid,part);
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write("");
        fileWriter.flush();
        fileWriter.close();

        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(partList);
        oos.close();
        fos.close();
        resp.sendRedirect(req.getContextPath()+"/partInfo.jsp");
    }
}
